/**
 * lilang
 * Org2TreeNode.java
 * 2013-9-12下午4:09:11
 */
package com.cq.oa.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import oa.base.util.tree.TreeNode;

import com.cq.oa.domain.sm.Org;
import com.cq.oa.domain.sm.Res;
import com.cq.oa.domain.sm.User;

/**
 * @author lilang
 *
 */
public class TreeNodeUtils {
	
	/**
	 * 组织转树节点
	 * @param orgs
	 * @return
	 * lilang 
	 * 2013-9-12下午4:10:38
	 */
	public static List<TreeNode> bulid2TreeNode(List<Org> orgs){
		List<TreeNode> nodes = new ArrayList<TreeNode>();
		for(Org org:orgs){
			TreeNode node = new TreeNode();
			node.setId(org.getSid());
			node.setText(org.getCname());
			node.setHasChild(true);
			node.getAttributes().put("orderBy", org.getOrderBy());
			node.getAttributes().put("msg", org.getMsg());
			node.getAttributes().put("email", org.getEmail());
			node.getAttributes().put("mobile", org.getMobile());
			node.getAttributes().put("upId", org.getUpId());
			node.getAttributes().put("orgSid", org.getOrgSid());
			nodes.add(node);
		}
		return nodes;
	}
	
	/**
	 * 组织转树节点
	 * @param orgs
	 * @return
	 * lilang 
	 * 2013-9-12下午4:10:38
	 */
	public static List<TreeNode> bulid2TreeNode(List<Org> orgs,Long sid){
		List<TreeNode> nodes = new ArrayList<TreeNode>();
		for(Org org:orgs){
			if(sid.equals(org.getUpId())){
				TreeNode node = new TreeNode();
				node.setId(org.getSid());
				node.setText(org.getCname());
				List<TreeNode> childs = bulid2TreeNode(orgs, org.getSid());
				node.setHasChild(childs.size()>0?true:false);
				node.setChildren(childs);
				node.getAttributes().put("orderBy", org.getOrderBy());
				node.getAttributes().put("msg", org.getMsg());
				node.getAttributes().put("email", org.getEmail());
				node.getAttributes().put("mobile", org.getMobile());
				node.getAttributes().put("upId", org.getUpId());
				node.getAttributes().put("orgSid", org.getOrgSid());
				nodes.add(node);
			}
		}
		return nodes;
	}
	
	/**
	 * 组织人员转树节点
	 * @param orgs
	 * @return
	 * lilang 
	 * 2013-9-12下午4:10:38
	 */
	public static List<TreeNode> bulidUser2TreeNode(List<User> users){
		List<TreeNode> nodes = new ArrayList<TreeNode>();
		for(User user:users){
			TreeNode node = new TreeNode();
			node.setId("u"+user.getSid());
			node.setText(user.getCname());
			node.setHasChild(false);
			node.getAttributes().put("orderBy", user.getOrderBy());
			node.getAttributes().put("email", user.getEmail());
			node.getAttributes().put("moblie", user.getMobile());
			node.getAttributes().put("username", user.getUsername());
			nodes.add(node);
		}
		return nodes;
	}
	
	
	
	/**
	 * 资源转树节点
	 * @param orgs
	 * @return
	 * lilang 
	 * 2013-9-12下午4:10:38
	 */
	public static List<TreeNode> bulidRes2TreeNode(List<Res> res){
		List<TreeNode> nodes = new ArrayList<TreeNode>();
		for(Res org:res){
			TreeNode node = new TreeNode();
			node.setId(org.getSid());
			node.setText(org.getCname());
			node.setHasChild(org.getIsLeaf()==0?true:false);
			node.getAttributes().put("orderBy", org.getOrderBy());
			node.getAttributes().put("msg", org.getMsg());
			node.getAttributes().put("pid", org.getPid());
			node.getAttributes().put("orgSid", org.getOrgSid());
			nodes.add(node);
		}
		return nodes;
	}
	/**
	 * 资源转树节点
	 * @param orgs
	 * @return
	 * lilang 
	 * 2013-9-12下午4:10:38
	 */
	public static List<TreeNode> bulidRes2TreeNode(List<Res> res,Long upId,Map<Long,Long> checkd){
		List<TreeNode> nodes = new ArrayList<TreeNode>();
		for(Res org:res){
			if(upId.equals(org.getPid())){
				TreeNode node = new TreeNode();
				node.setId(org.getSid());
				node.setText(org.getCname());
				node.setHasChild(org.getIsLeaf()==0?true:false);
				if(checkd.get(org.getSid())!=null){
					node.setChecked(true);
				}
				node.setState(TreeNode.STATE_OPEN);
				node.getAttributes().put("orderBy", org.getOrderBy());
				node.getAttributes().put("msg", org.getMsg());
				node.getAttributes().put("pid", org.getPid());
				node.getAttributes().put("orgSid", org.getOrgSid());
				node.setChildren(bulidRes2TreeNode(res, org.getSid(),checkd));
				nodes.add(node);
			}
		}
		return nodes;
	}

}
